package com.metagurukul.informer.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.metagurukul.informer.service.LoginService;
import com.metagurukul.informer.service.RegisterUserService;
import com.metagurukul.informer.utility.ExceptionProperties;

/**
 * Servlet implementation class ChangePassword
 */
@WebServlet("/ChangePassword")
public class ChangePassword extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
 

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPerform(request,response);
	}

	private void doPerform(HttpServletRequest request,
		HttpServletResponse response)throws ServletException, IOException  {
		Map<String, String> exceptionmap = new HashMap<String, String>();
		try {
			String oldPassword=request.getParameter("oldpassword");
			String newPassword=request.getParameter("newpassword");
			HttpSession session=request.getSession();
			String id=null;
			if(session.getAttribute("adminName")!=null){
				id=(String)session.getAttribute("adminName");
			}
			else if(session.getAttribute("userName")!=null){
				id=(String)session.getAttribute("userName");
			}
			else{
				response.sendRedirect("ShowQuestions");
			}
			if(oldPassword!=null){
			 	if(!oldPassword.trim().equals("")){
			 		LoginService loginservice = new LoginService(id, oldPassword);
					String result[]=loginservice.doLogin();
					if(result[0]!=null){
						RegisterUserService registerUserService=new RegisterUserService(id, null ,newPassword);
						if(registerUserService.update()){
							exceptionmap.put("password", "PassWord Updated Sucessfully !!");
							request.setAttribute("exceptionmap",exceptionmap);
							RequestDispatcher rd=request.getRequestDispatcher("ResetPassword.jsp");
							rd.forward(request, response);
						}else{
							exceptionmap.put("password", "Password could not be updated");
							request.setAttribute("exceptionmap",exceptionmap);
							RequestDispatcher rd=request.getRequestDispatcher("ResetPassword.jsp");
							rd.forward(request, response);
						}
					}
					else{
						exceptionmap.put("password", ExceptionProperties._112);
						request.setAttribute("exceptionmap",exceptionmap);
						RequestDispatcher rd=request.getRequestDispatcher("ResetPassword.jsp");
						rd.forward(request, response);
					}
			    }
			 	else{
			 		response.sendRedirect("Error.jsp");
			 	}
			 }
		} catch (SQLException e) {
			response.sendRedirect("Error.jsp");
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPerform(request,response);
	}

}
